package com.nlgc365.designpattern.Chapter04_Pattern.Creation.P01_Singleton.Usage.IDGenerators;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 单例模式-饿汉式
 * 功能: ID自增生成器
 */
public class IdGenerator1 {

    private AtomicLong atomicLong = new AtomicLong(0);

    //2, 类加载即创建该实例
    private static IdGenerator1 idGenerator= new IdGenerator1();

    //1, 私有构造函数
    private IdGenerator1() {
    }

    //3, 提供一个返回单例实例的接口
    public static IdGenerator1 getInstance() {
        return idGenerator;
    }

    /**
     * 生成自增ID
     *
     * @return 返回ID
     */
    public long getId() {
        return atomicLong.getAndIncrement();
    }
}
